package com.ts.api.module.api.convert.market.convert;

import com.gitee.apanlh.util.base.Eq;
import com.ts.api.module.api.convert.core.annotation.ApiConsumerMarket;
import com.ts.api.module.api.convert.core.annotation.ApiConsumerMarketConvertType;
import com.ts.api.module.api.convert.core.context.ApiConvertRegister;
import com.ts.api.module.api.convert.market.handler.ConsumerMarketConvertHandler;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 *  下游贷超转换上下文-卖量
 *
 *  @author Pan
 */
@Component
public class ConsumerMarketConvert extends ApiConvertRegister<ConsumerMarketConvertHandler, ApiConsumerMarket> {

    /**
     *  默认构造函数
     */
    public ConsumerMarketConvert() {
        super(
            ConsumerMarketConvertHandler.class,
            ApiConsumerMarket.class
        );
    }

    @Override
    public void registerHandler(ApplicationContext applicationContext, Map<String, ConsumerMarketConvertHandler> contextCache) {
        ApiConsumerMarketConvertType[] values = ApiConsumerMarketConvertType.values();
        //  枚举方式
        for (ApiConsumerMarketConvertType apiConsumerMarketConvertType : values) {
            if (Eq.enums(ApiConsumerMarketConvertType.NULL, apiConsumerMarketConvertType)) {
                continue;
            }

            String prefixBeanName = apiConsumerMarketConvertType.getPrefixBeanName();
            classRegister(prefixBeanName, (annotation) -> Eq.enums(annotation.type(), apiConsumerMarketConvertType));
            factoryRegister(prefixBeanName, (annotation) -> Eq.enums(annotation.type(), apiConsumerMarketConvertType));
        }
    }

    @Override
    public String extractKey(ApiConsumerMarket annotation) {
        return annotation.marketId();
    }

    /**
     *  根据枚举获取处理器
     *
     *  @param  marketId                           下游贷超枚举
     *  @param  apiConsumerMarketConvertType       下游贷超枚举
     *  @return ConsumerMarketConvertHandler
     */
    public ConsumerMarketConvertHandler getHandler(Long marketId, ApiConsumerMarketConvertType apiConsumerMarketConvertType) {
        return super.getHandler(apiConsumerMarketConvertType.getPrefixBeanName() + marketId);
    }

    /**
     *  根据枚举获取处理器
     *
     *  @param  apiConsumerMarketConvertType       下游贷超枚举
     *  @return ConsumerMarketConvertHandler
     */
    public ConsumerMarketConvertHandler getDefaultHandler(ApiConsumerMarketConvertType apiConsumerMarketConvertType) {
        return super.getHandler(apiConsumerMarketConvertType.getPrefixBeanName() + apiConsumerMarketConvertType.getDefaultBeanName());
    }
}
